package com.qin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qin.db.DBAccess;
import com.qin.enums.RetCode;
import com.qin.entity.TSystem;
import com.qin.mapper.SysMapper;
import com.qin.service.SysService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysServiceImpl implements SysService {

    @Override
    public JSONObject querySystems(TSystem tSystem) {

//        System.out.println("service层：querySystems");

        //调用Dao层
        SqlSession sqlSession = null;
        Map map = new HashMap();
        JSONArray retArray;

        try {
            sqlSession = DBAccess.getSqlSession();
            SysMapper sysMapper = sqlSession.getMapper(SysMapper.class);

            List retList = sysMapper.querySystems(tSystem);
            retArray = new JSONArray(retList);

        } catch (Exception e) {
            e.printStackTrace();
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", e.getMessage());
            JSONObject retJson = new JSONObject(errMap);

            return retJson;
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }

        //拼接返回JSON
        map.put("sysInfo", retArray);
        map.put("retCode", RetCode.SUCCESS.getCode());
        map.put("retMsg", RetCode.SUCCESS.getMsg());
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }
}
